<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AccountManagement;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\AccountCategory ;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Admin;
use Encore\Admin\Grid\Filter;
use App\Models\Product;

class AccountManagementController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AccountManagement(), function (Grid $grid) {
            $grid->tableCollapse(false);
            $userId = Admin::user()->id;
            if ($userId == 1) {
                // 当 $userId 等于 1 时不执行 where 条件
            } else {
                $grid->model()->where('User', $userId);
            }
            $grid->column('id')->sortable();
            $grid->column('account_type', '账户类型')->display(function ($value) {

                // 调用模型的访问器方法获取对应的颜色
                $color = $this->getAccountTypeColorAttribute();
            
                return "<span style='background-color: $color;color: white;'>$value</span>"; // 将颜色应用到渲染结果
            });
            

            $grid->column('account_id');
            $grid->column('account_password');
            $grid->column('permission');

            $grid->column('user')->display(function ($AdministratorId) {
                $Administrator = Administrator::find($AdministratorId);
                return $Administrator ? $Administrator->name : '';
            });
           
            $grid->column('email');
            $grid->column('phone');
            $grid->column('usage');
            $grid->column('url');
            $grid->column('remark');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('user')->select(Administrator::pluck('name', 'id'))->width(2);
                $filter->equal('account_type')->select(AccountCategory::pluck('name', 'name'))->width(2);
                //$phones = AccountManagement::pluck('account_id')->toArray();

                $filter->equal('account_id')->width(2);
                $filter->like('permission')->width(2);
                $filter->like('email')->width(1);
                
                $filter->like('phone')->width(1);
                $filter->like('usage')->width(1);



        
            });

             //开启弹窗创建表单
             $grid->enableDialogCreate();
             // 显示快捷编辑按钮
             $grid->showQuickEditButton();
             // 禁用编辑按钮
             $grid->disableEditButton(); 
             //设置列选择器
             $grid->showColumnSelector();
             $grid->hideColumns(['created_at','updated_at','id',]);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AccountManagement(), function (Show $show) {
            $show->field('id');
            $show->field('account_type');
            $show->field('account_id');
            $show->field('account_password');
            $show->field('permission');
            $show->field('user');
            $show->field('email');
            $show->field('phone');
            $show->field('usage');
            $show->field('url');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AccountManagement(), function (Form $form) {
            $form->display('id');
            $form->select('account_type', '账号类型')->options(function () {
                return AccountCategory ::pluck('name', 'name');
            })->required();
            $form->text('account_id')->required();
            $form->text('account_password')->required();

            $form->select('permission', '权限')->options([
                '普通' => '普通',
                '管理' => '管理',
                ])->rules('required');
            $form->select('user', '用户')->options(function () {
                    return Administrator::pluck('name', 'id');
                });
        
            $form->text('email');
            $form->text('phone');
            $form->textarea('usage')->rows(5);
            $form->text('url');
            $form->textarea('remark')->rows(5);
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
